package com.quzhi.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quzhi.common.code.Code;
import com.quzhi.sys.entity.GoodsSerialNumber;
import com.quzhi.sys.mapper.GoodsSerialNumberMapper;
import com.quzhi.sys.service.IGoodsSerialNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xibing
 * @since 2024-08-11
 */
@Service
public class GoodsSerialNumberServiceImpl extends ServiceImpl<GoodsSerialNumberMapper, GoodsSerialNumber> implements IGoodsSerialNumberService {


    @Override
    public String addGoodsSerialNumber(String SerialNumber, String ReceiveGoodsId) {
        String message = "";
        // 查询是否有重复序列号(所有待入库的)
        LambdaQueryWrapper<GoodsSerialNumber> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsSerialNumber::getSerialNumber, SerialNumber);
        wrapper.eq(GoodsSerialNumber::getStatus, Code.DAI_RU_KU);
        GoodsSerialNumber one = this.baseMapper.selectOne(wrapper);

        if (one != null) return one.getSerialNumber() + ",已添加序列号，未入库";


        GoodsSerialNumber goodsSerialNumber = new GoodsSerialNumber();
        goodsSerialNumber.setId(UUID.randomUUID().toString());
        goodsSerialNumber.setSerialNumber(SerialNumber);
        goodsSerialNumber.setReceiveGoodsId(ReceiveGoodsId);
        goodsSerialNumber.setCreateTime(LocalDateTime.now());


        this.baseMapper.insert(goodsSerialNumber);
        return message;
    }


    /**
     * 获取指定状态的序列号列表
     */
    @Override
    public List<GoodsSerialNumber> getGoodsSerialNumberList(Boolean status) {

        LambdaQueryWrapper<GoodsSerialNumber> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsSerialNumber::getStatus, status);

        return this.baseMapper.selectList(lambdaQueryWrapper);

    }
}
